<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Product\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\TableGateway\TableGateway;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;

class ProductModel {

    const tbl_product = 'zend_product';
    const tbl_category = 'zend_product_category';
    const tbl_maker = 'zend_product_maker';
    const tbl_rlt_category_maker = 'zend_category_maker';

    private $adapter;
    private $productTableGateway;
    private $categorTableGateway;
    private $makerTableGateway;

    public function __construct(Adapter $adapter) {

        $this->adapter = $adapter;

        $this->productTableGateway = new TableGateway(self::tbl_product, $this->adapter);

        $this->categorTableGateway = new TableGateway(self::tbl_category, $this->adapter);

        $this->makerTableGateway = new TableGateway(self::tbl_maker, $this->adapter);
    }

    public function getProductCategory() {

        $select = $this->categorTableGateway->getSql()->select();

        $select->columns(array('category_id',
            'category_name',
            'category_name_filter'));

        $select->where(array('category_del_flag = ?' => 0));

        $result = $this->categorTableGateway->selectWith($select);

        return $result->toArray();
    }

    public function getProductByType($type, $limit) {

        $select = $this->productTableGateway->getSql()->select();

        $select->columns(array('product_id',
            'product_name',
            'product_name_filter',
            'product_price',
            'product_price_sale_of',
            'product_image'));

        $select->where(array('product_del_flag = ?' => 0));

        $select->where(array('product_type = ?' => $type));

        $select->limit($limit);

        $select->order(array('product_update_date' => 'DESC'));

        $result = $this->productTableGateway->selectWith($select);

        return $result->toArray();
    }

    public function getProductDetail($productId) {

        $select = $this->productTableGateway->getSql()->select();
        $select->columns(array('product_name',
            'product_price',
            'product_price_sale_of',
            'product_image',
            'product_category_name',
            'product_category_id',
            'product_category_name_filter',
            'product_maker_maker_name',
            'product_content_detail',
            'product_maker_maker_name_filter',
            'product_spec_content',
            'product_content_detail',
        ));
        $select->join(self::tbl_category, 'product_category_id = category_id', array('category_id', 'category_name', 'category_name_filter'), \Zend\Db\Sql\Select::JOIN_LEFT);

        $select->join(self::tbl_maker, 'product_maker_maker_id = product_maker_id', array('product_maker_name'), \Zend\Db\Sql\Select::JOIN_LEFT);

        $select->where(array('product_del_flag = ?' => 0, 'product_id = ?' => $productId));

        $result = $this->productTableGateway->selectWith($select);

        return $result->toArray();
    }

    public function getListProduct($aryConditions) {
        $select = $this->productTableGateway->getSql()->select();
        $select->columns(array('product_id',
            'product_image',
            'product_name',
            'product_name_filter',
            'product_price',
            'product_price_sale_of'));

        $select->where(array('product_del_flag = ?' => 0));

        if (isset($aryConditions['category_id']) && is_numeric($aryConditions['category_id'])) {
            $select->where(array('product_category_id = ?' => $aryConditions['category_id']));
        }

        if (isset($aryConditions['maker_name']) && $aryConditions['maker_name'] != 'all') {
            $select->where(array('product_maker_maker_name_filter = ?' => $aryConditions['maker_name']));
        }

        if ($aryConditions['price_from'] >= 0 && $aryConditions['price_to'] > 0 && $aryConditions['price_to'] > $aryConditions['price_from']) {
            $select->where->between('product_price', (int) $aryConditions['price_from'], (int) $aryConditions['price_to']);
        }

        $select->order(array('product_price' => $aryConditions['order']));

        $paginatorAdapter = new DbSelect($select, $this->adapter);

        $paginator = new Paginator($paginatorAdapter);

        $paginator->setCurrentPageNumber($aryConditions['current_page']);

        $paginator->setItemCountPerPage($aryConditions['limit']);

        return $paginator;
    }

    public function getMaker($categoryId) {

        $select = $this->makerTableGateway->getSql()->select();

        $select->columns(array('product_maker_id',
            'product_maker_name',
            'product_maker_name_filter'));
        $select->join(self::tbl_rlt_category_maker, 'product_maker_id = relation_maker_id', array(), \Zend\Db\Sql\Select::JOIN_RIGHT);

        $select->where(array('product_maker_del_flag = ?' => 0));

        $select->where(array('relation_category_id = ?' => $categoryId));

        $result = $this->makerTableGateway->selectWith($select);

        return $result->toArray();
    }

    public function searchProduct($aryConditions) {

        $select = new \Zend\Db\Sql\Select();

        $select->from(self::tbl_product);

        $select->columns(array('product_id',
            'product_name',
            'product_name_filter',
            'product_price',
            'product_price_sale_of',
            'product_image',
            'product_spec_content'
        ));
        $select->where(array('product_del_flag = ?' => 0));

        if (isset($aryConditions['keyword']) && $aryConditions['keyword'] != 'all') {
            $select->where->like('product_name_search', '%' . $aryConditions['encode_keyword'] . '%');
        }

        $paginatorAdapter = new DbSelect($select, $this->adapter);

        $paginator = new Paginator($paginatorAdapter);

        $paginator->setCurrentPageNumber($aryConditions['current_page']);
        $paginator->setItemCountPerPage($aryConditions['record_per_page']);

        return $paginator;
    }

}
